import ajax from "../../js/ajax.js";


let txtphone=document.getElementById('txtphone');
let txtcode=document.getElementById('txtcode');
let btncode=document.getElementById('btncode');
let btnbd=document.getElementById('btnbd');
let txtimageCode = document.getElementById('txtimageCode');
let imageCode = document.getElementById('imageCode');

//#region 绑定手机信息


btncode.addEventListener('click',()=>{
  ajax.send(
    '/tool/sendValidateCode',
    {
      phone: txtphone.value,
      imageCode:txtimageCode.value,
    },
    (data) => {
      showToast(data.message);
    }
  );
})

btnbd.addEventListener('click',()=>{
  if (txtcode.value.trim() == '') {
    showToast('验证码不能为空');
    return;
  }

  let cs = {
    code: txtcode.value,
    phone: txtphone.value
  };

  ajax.send('/user/auth/updateUserPhone',cs,(data)=>{
    showToast(data.message);
  })
   //成功跳转
  location.href='./ym.html';
})


//#endregion

//#region 获取图片验证码
//通过ajax获取图片验证码的function

function loadImgCode() {
  ajax.send('/tool/getImageCode', {}, (info) => {
    imageCode.setAttribute('src', info.message);
  });
}

imageCode.addEventListener('click', loadImgCode);

loadImgCode();

btncode.addEventListener('click', () => {
  //验证图片验证码
  ajax.send(
    '/tool/checkImageCode',
    {
      code: txtimageCode.value,
    },
    (info) => {
      alert(info.message);
      //不论正确与否，验证码只能使用1次
      //必须重新更新,验证码不区分大小写
      loadImgCode();
    }
  );
});

//#endregion






//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion
